package com.nenude.corejava.ch9.homework;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class TestDate {

	public static void main(String[] args) throws ParseException {
		System.out.println("请输入商品的生产日期：(yyyy-MM-dd)");
		Scanner scanner = new Scanner(System.in);
		// 转换格式:输入的日期
		String outdata = scanner.nextLine();

		// 保质期
		System.out.println("请输入保质期(天)：");
		int enddata = scanner.nextInt();

		// 时间与时间字符串间的转换
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		// 获取系统当前时间
		System.out.println("今天日期为：");
		Date now = new Date();
		String nowDate = sdf.format(now);
		System.out.println(nowDate);

		// 日历类
		Calendar calendar = Calendar.getInstance();//实例化日历
		Date inDate = sdf.parse(outdata);
		calendar.setTime(inDate);

		// 加上30天
		calendar.add(Calendar.DATE, enddata);

		String outdateString = sdf.format(calendar.getTime());

		System.out.println("过期的日期为：" + outdateString);

		// 判断是否超出日期
		Date endDate = sdf.parse(outdata);
		boolean before = now.before(endDate);
		if (before == true) {
			calendar.setTime(now);
			// unix 时间--在有些数据库设计中，使用long保存时间
			long time1 = calendar.getTimeInMillis();

			calendar.setTime(endDate);
			long time2 = calendar.getTimeInMillis();

			long between_days = (time2 - time1) / (1000 * 3600 * 24);
			System.out.println("还有" + between_days + "过期");
		} else {
			System.out.println("该商品过期");
		}

	}

}
